package com.woniu.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.woniu.entity.PageResult;
import com.woniu.entity.Result;
import com.woniu.vo.SearchCriteria;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.woniu.pojo.PackageItemContact;
import com.woniu.service.IPackageItemContactService;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 服务包与项目关联 前端控制器
 * </p>
 *
 * @author YMY
 * @since 2022-10-12
 */
@Api(tags = "服务项目与服务包关联")
@RestController
@RequestMapping("/package-item-contact")
    public class PackageItemContactController extends BaseController{

        @Autowired
        private IPackageItemContactService packageItemContactService;

        @ApiOperation("查找所有服务包与服务项目关联")
        @GetMapping
        public Result queryAll(){
            List<PackageItemContact> packageItemContactList = packageItemContactService.list();

            return toDataResult(packageItemContactList) ;

        }

        @ApiOperation("增加服务包的服务项目")
        @PostMapping
        public Result addItemsByPackage(@RequestParam Integer packageId,@RequestBody List<String> items){
                List<PackageItemContact> list=new ArrayList<>();
                items.forEach(item->{
                    PackageItemContact contact=new PackageItemContact();
                    contact.setItemId(Long.valueOf(item));
                    contact.setPackageId(Long.valueOf(packageId));
                    list.add(contact);
                });
            return toResult(packageItemContactService.saveBatch(list));

        }

        @ApiOperation("删除服务包的所有服务项目")
        @DeleteMapping("/{id}")
        public Result deleteByPackageId(@PathVariable Integer id){

            Map<String,Object> map=new HashMap<>();
            map.put("package_id",Long.valueOf(id+""));
            return toResult(packageItemContactService.removeByMap(map));
        }



        @ApiOperation("条件分页查找")
        @PostMapping("/search/{page}/{size}")
        public Result search(@PathVariable int page, @PathVariable int size, @RequestBody SearchCriteria searchCriteria){
            IPage<PackageItemContact> p=new Page<PackageItemContact>(page,size);
            QueryWrapper<PackageItemContact> wrapper=new QueryWrapper<>();
            if(searchCriteria.getName()!=null)wrapper.like("name",searchCriteria.getName());
            if(searchCriteria.getStartTime()!=null)wrapper.gt("create_time",searchCriteria.getStartTime());
            if(searchCriteria.getEndTime()!=null)wrapper.lt("create_time",searchCriteria.getEndTime());
            if(searchCriteria.getOther()!=null&&searchCriteria.getOther().size()>0){
                searchCriteria.getOther().forEach((key,value)->{
                wrapper.eq(key,value);
                });
            }
            if(searchCriteria.getOtherLike()!=null&&searchCriteria.getOtherLike().size()>0){
                searchCriteria.getOtherLike().forEach((key,value)->{
                wrapper.like(key,value);
                });
            }

            p = packageItemContactService.page(p, wrapper);
            PageResult pageResult=new PageResult();
            pageResult.setData(p.getRecords());
            pageResult.setTotal(p.getTotal());

            return toDataResult(pageResult);
        }





        }

